package abc.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Slf4j
public class KillPortUtil {

//    public static void main(String[] args) throws IOException {
//        int i = Integer.parseInt(args[0]);
//        kill(false, i);
//    }

    /**
     * 结束占用端口的进程
     * isForce = true 时
     * return = KILLED(成功杀死进程)，FAIL(杀死进程失败或不存在该进程)
     * isForce = false 时
     * return = AVAILABLE(成功杀死CLOSE_WAIT进程，由于不存在TCP:LISTEN 的进程，所以可以尝试重复占用该端口)，KILLED(成功杀死TCP:CLOSE_WAIT的进程)，FAIL(杀死进程失败或不存在该进程)
     */
    public static KillResult kill(boolean isForce, int port) throws IOException {
        Set<Integer> ports = new HashSet<>();
        // 将要关闭的端口添加到set中
        ports.add(port);
        // 判断linux环境
        Boolean isLinux = isOSLinux();
        // 查询端口命令 linux 与 windows区分
        String[] command = isLinux ? new String[]{"/bin/sh", "-c", "netstat -anp |grep " + port} : new String[]{"cmd /c netstat -ano | findstr " + port};
        // 读取内容
        List<String> read = execAndRead(command, isLinux, ports);
        if (read.size() == 0) {
            log.info("未查询到端口被占用");
            return KillResult.FAIL;
        } else {
            // 关闭端口
            return kill(isForce, read, isLinux, port);
        }
    }

    // 执行命令并且读取结果
    private static List<String> execAndRead(String[] command, Boolean isLinux, Set<Integer> ports) throws IOException {
        // 读取结果
        List<String> lines = new ArrayList<String>();
        if (!isLinux) {
            //自己实现

        } else {
            Runtime runtime = Runtime.getRuntime();
            //查找进程号
            log.info("执行命令:{}", command[2]);
            Process p = runtime.exec(command);
            InputStream inputStream = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            try {
                String line;
                int i = 1;
                while ((line = reader.readLine()) != null) {
                    // 验证端口
                    log.info("执行结果[{}]:{}", i++, line);
                    boolean validPort = validPort(line, isLinux, ports);
                    if (validPort) {
                        // 添加内容
                        lines.add(line);
                    }
                }
                inputStream.close();
                reader.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                inputStream.close();
                reader.close();
            }
        }
        return lines;
    }

    /**
     * 验证此行是否为指定的端口，因为 findstr命令会是把包含的找出来，例如查找80端口，但是会把8099查找出来
     *
     * @param str
     * @return
     */
    private static boolean validPort(String str, Boolean isLinux, Set<Integer> ports) {
        String find;
        // linux TCP    0.0.0.0:12349          0.0.0.0:0              LISTENING       30700
        // windows tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      2319/python
        String reg = isLinux ? ":[0-9]+" : "^ *[a-zA-Z]+ +\\S+";
        // 匹配正则
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            // 获取匹配内容
            find = matcher.group(0);
            // 处理数据
            int spStart = find.lastIndexOf(":");
            // 截取掉冒号
            find = find.substring(spStart + 1);
            int port = 0;
            try {
                port = Integer.parseInt(find);
                // 端口在其中 则通过验证
                if (ports.contains(port)) {
                    return true;
                }
            } catch (NumberFormatException e) {
                log.warn(e.getMessage());
            }
        }
        return false;
    }

    /**
     * 更换为一个Set，去掉重复的pid值
     *
     * @param data
     */
    private static KillResult kill(boolean isForce, List<String> data, Boolean isLinux, int port) throws IOException {
        // linux   tcp6       0      0 :::9011                 :::*                    LISTEN      22760/java
        // windows tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      2319/python
        Set<Integer> pids = new HashSet<>();
        boolean existListen = false;

        for (String line : data) {

            if (isLinux && !isForce) {

                if (line.contains("LISTEN") || line.contains("ESTABLISHED")) {
                    existListen = true;
                }

                //isForce=false 只能杀死端口状态CLOSE_WAIT的进程
                if (!line.contains("CLOSE_WAIT")) {
                    continue;
                }

            }
            // 去除前后空格
            line = line.trim();
            // 获取最后一个空格下标
            int offset = line.lastIndexOf(" ");
            // 截取最后的内容 如 30700 或者 2319/python
            String spid = line.substring(offset);
            // 替换其中的空格
            spid = spid.replaceAll(" ", "");
            // 如果存在/
            int lastSlashIndex = spid.lastIndexOf("/");
            if (lastSlashIndex != -1) {
                // 处理/
                spid = spid.substring(0, lastSlashIndex);
            }
            try {
                int pid;
                pid = Integer.parseInt(spid);
                pids.add(pid);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        KillResult killResult;
        if (CollectionUtil.isNotEmpty(pids)) {
            log.info("需要关闭的pid:" + pids);
            if (killWithPid(pids, isLinux)) {
                killResult = !existListen ? KillResult.AVAILABLE : KillResult.KILLED;
            } else {
                killResult = KillResult.FAIL;
            }
        } else {
            log.info("未查询到可以被结束的进程");
            //没有可结束的进程只能返回AVAILABLE、FAIL
            killResult = !existListen ? KillResult.AVAILABLE : KillResult.FAIL;
        }
        if (KillResult.AVAILABLE.equals(killResult)) {
            log.info("由于不存在TCP:LISTEN 的进程，所以可以尝试强行占用{}端口", port);
        }
        return killResult;
    }

    /**
     * 一次性杀除所有的端口
     *
     * @param pids
     */
    private static boolean killWithPid(Set<Integer> pids, Boolean isLinux) throws IOException {
        if (isLinux) {
            int count = 0;
            for (Integer pid : pids) {
                String commond = "kill -9 " + pid;
                log.info("执行命令:" + commond);
                Process process = Runtime.getRuntime().exec(commond);
                InputStream inputStream = process.getInputStream();
                String txt = readTxt(inputStream, "GBK");
                if (StringUtils.isEmpty(txt)) {
                    log.info("执行结果:成功");
                } else {
                    log.info("执行结果:" + txt);
                }
                count++;
            }
            return count > 0;

        } else {
//            自己实现;
            return false;
        }
    }


    private static List<String> read(String outPut, Boolean isLinux, Set<Integer> ports) throws IOException {
        List<String> data = new ArrayList<>();
        // 获取换行符
        String lineSeparator = System.lineSeparator();
        // 换行
        String[] lineArray = outPut.split(lineSeparator);
        if (lineArray.length > 0) {
            for (int i = 0; i < lineArray.length; i++) {
                String line = lineArray[i];
                // 验证端口
                boolean validPort = validPort(line, isLinux, ports);
                if (validPort) {
                    // 添加内容
                    data.add(line);
                }
            }
        }
        return data;
    }

    private static boolean isOSLinux() {
        Properties prop = System.getProperties();
        String os = prop.getProperty("os.name");
        return os != null && os.toLowerCase().contains("linux");
    }

    private static String readTxt(InputStream in, String charset) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        return sb.toString();
    }


    /**
     * 结束占用端口的进程
     * isForce = true 时
     * return = KILLED(成功杀死进程)，FAIL(杀死进程失败或不存在该进程)
     * isForce = false 时
     * return = AVAILABLE(成功杀死CLOSE_WAIT进程，由于不存在TCP:LISTEN 的进程，所以可以尝试重复占用该端口)，KILLED(成功杀死TCP:CLOSE_WAIT的进程)，FAIL(杀死进程失败或不存在该进程)
     */
    public enum KillResult {
        AVAILABLE,
        KILLED,
        FAIL;
    }
}
